﻿using HomagGroup.Base.UI.Controls;
using PropertiesLibarary.Properties;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using localText = PropertiesLibarary.Properties.Resources;
using ScrollViewer = System.Windows.Controls.ScrollViewer;

namespace HomagChina.Nesting.Views
{
    /// <summary>
    /// Home.xaml 的交互逻辑
    /// </summary>
    public partial class Home : UserControl
    {
        private double HomeBatchDetailVerticalOffset;
        private double HomeBatchVerticalOffset;

        public Home()
        {
            InitializeComponent();
        }

        public void ViewModel_RequestScrollPositionSave(object sender, EventArgs e)
        {
            SaveScrollPosition();
        }

        public void ViewModel_RequestScrollPositionRestore(object sender, EventArgs e)
        {
            RestoreScrollPosition();
        }

        private void SaveScrollPosition()
        {
            var BatchDetailscrollViewer = VisualTreeHelperExtensions.FindVisualChild<ScrollViewer>(HomeBatchDetail);
            //var BatchscrollViewer = VisualTreeHelperExtensions.FindVisualChild<ScrollViewer>(HomeBatch);
            if (BatchDetailscrollViewer != null)// && BatchscrollViewer != null
            {
                HomeBatchDetailVerticalOffset = BatchDetailscrollViewer.VerticalOffset;
                //HomeBatchVerticalOffset = BatchscrollViewer.VerticalOffset;
            }
        }

        private void RestoreScrollPosition()
        {
            var BatchDetailscrollViewer = VisualTreeHelperExtensions.FindVisualChild<ScrollViewer>(HomeBatchDetail);
            //var BatchscrollViewer = VisualTreeHelperExtensions.FindVisualChild<ScrollViewer>(HomeBatch);
            BatchDetailscrollViewer?.ScrollToVerticalOffset(HomeBatchDetailVerticalOffset);
        }

        private void Table_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var RouterDictionary = (Dictionary<string, RouterItemDataModel>)Application.Current.Properties["RouterMap"];
            FunctionKeyBarItemDataModel ActionItem = RouterDictionary["1"].functionKeyBarItems[5];
            HomeViewModel homeViewModel = RouterDictionary["1"].userControl.DataContext as HomeViewModel;
            if (sender is Table row && homeViewModel != null)
            {
                if (row.CurrentCell.Column.Header?.ToString() == "ID")
                {
                    if (ActionItem.Header != localText.Action && ActionItem.Header != "开始")
                    {
                        homeViewModel.HandleClickMprItem(int.Parse(row.CurrentCell.Value.ToString()));
                    }
                    else
                    {
                        HomagGroup.Base.UI.Windows.MessageBox.Show("请先开启任务！");
                    }
                }
            }
        }

        public static class VisualTreeHelperExtensions
        {
            public static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
            {
                if (parent == null) return null;

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    if (child is T foundChild)
                    {
                        return foundChild;
                    }
                    var childOfChild = FindVisualChild<T>(child);
                    if (childOfChild != null)
                    {
                        return childOfChild;
                    }
                }
                return null;
            }
        }
    }
}